package controller;

import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/session")
@SessionAttributes("user")
public class SessionController {

    @Autowired
    private HttpSession session;

    @RequestMapping("/f1")
    public String f1(HttpSession session){
        System.out.println("SessionController.f1()");

        User user=new User();
        user.setUsername("aaa");

        session.setAttribute("user",user);

        return "session";
    }

    @RequestMapping("/f2")
    public String f2(HttpSession session){
        System.out.println("SessionController.f2()");

        User user= (User) session.getAttribute("user");

        System.out.println("user = " + user);

        return "session";
    }

    @RequestMapping("/f3")
    public String f3(User user){
        System.out.println("SessionController.f3()");

        System.out.println("user = " + user);

        user.setUsername("bbb");

        return "session";
    }

    @RequestMapping("/f4")
    public String f4(User user){
        System.out.println("SessionController.f4()");

        System.out.println("user = " + user);

        return "session";
    }

    @RequestMapping("/f5")
    public String f5(@ModelAttribute User user){
        System.out.println("SessionController.f5()");

        System.out.println("user = " + user);

        return "session";
    }

    @RequestMapping("/f6")
    public String f6(@ModelAttribute("u") User user){
        System.out.println("SessionController.f6()");

        System.out.println("user = " + user);

        user.setUsername("ccc");

        return "session";
    }

    @RequestMapping("/f7")
    public String f7(User user,HttpSession session){
        System.out.println("SessionController.f7()");

        System.out.println("user = " + user);

        session.invalidate();

        return "session";
    }

    @RequestMapping("/f8")
    public String f8(User user, SessionStatus sessionStatus){
        System.out.println("SessionController.f7()");

        System.out.println("user = " + user);

        sessionStatus.setComplete();

        return "session";
    }
    
    @RequestMapping("/f9")
    public String f9(){
        System.out.println("SessionController.f9()");
        User user=new User();
        user.setUsername("ddd");
        session.setAttribute("user",user);
        return "session";
    }

    @RequestMapping("/f10")
    public String f10(){
        System.out.println("SessionController.f10()");
        User user= (User) session.getAttribute("user");
        System.out.println("user = " + user);
        return "session";
    }

}
